clear
capture log close

log using LogFile_PreparedPublicReplicationFileStudy1.txt, replace text

* This .do file loads the raw .csv export from qualtrics for Study 1
* It performs recodes and anonymizes the data
* The raw data (identified, with IPs/etc.) is NOT included in the replication file.
* The outputted file from this code is Study1data_publicdatafile_cleaned.dta

* Load Study 1 data
insheet using "..\PrivateDataNotInReplicationArchive\Study1Police Interaction Vignettes April 2021_May 12, 2021_15.17.csv", names

* Drop items from unrelated study
drop orr_*

gen study="Study1"

* Get rid of test cases
drop if rid==""
drop if supname=="Z-TestSupplier"
drop if supname==""
keep if status=="IP Address"
keep if updated=="TRUE"

* Must consent
keep if intro_prompt =="I agree to participate"

* Filter on passing pre-treatement attention check
keep if huber_acq_identify=="Because he left his ID"

* Recode treatment variables
gen treat_officerwhite = z_cop_race=="White"
label var treat_officerwhite "Officer White (1=yes)"
label def lblofrace 0 "Black" 1 "White"
label values treat_officerwhite lblofrace

gen S1treat_demeanor=.
replace S1treat_demeanor=1 if z_interaction=="AP:AP"
replace S1treat_demeanor=2 if z_interaction=="AP:AH"
replace S1treat_demeanor=3 if z_interaction=="BH:AH"
replace S1treat_demeanor=4 if z_interaction=="AH:AH"
replace S1treat_demeanor=5 if z_interaction=="AH:AP"
label var S1treat_demeanor "Demeanor"
label define S1lbldemeanor 1 "Both Polite" 2 "Hostile Civilian, Polite Officer" 3 "Both Hostile (Civilian first)" 4 "Both Hostile (Officer first)" 5 "Polite Civilian, Hostile Officer" 
label values S1treat_demeanor S1lbldemeanor

gen treat_ticket = z_outcome=="ticket"
label var treat_ticket "Assigned punishment (1=ticketed)"
label define labticket 0 "Escorted from park" 1 "Ticketed"
label values treat_ticket labticket

* Ticket demeanor interactions
gen alt2_treat_tic_dmnr=(S1treat_demeanor-1)*treat_ticket
label define alt2intlabel 0 "Ticketed x Both Polite" 1 "Ticketed x Hostile Civilian, Polite Officer" 2 "Ticketed x Both Hostile (Civilian first)" 3 "Ticketed x Both Hostile (Officer first)" 4 "Ticketed x Polite Civilian, Hostile Officer" 
label values alt2_treat_tic_dmnr alt2intlabel

* Recode outcomes

* Expected punishment

tab vig_predict_1
local values_temp `" "Officer Simmons left after speaking with Parsons" "Parsons was escorted from the park" "Parsons was escorted from the park and given a verbal warning" "Parsons was escorted from the park and issued a citation for disorderly conduct" "Parsons was arrested for disorderly conduct" "'
local ctr_temp=1
gen S1out_expectedpunish_vig1 = .
foreach v of local values_temp {
	di "--`v'--"
	replace S1out_expectedpunish_vig1=`ctr_temp' if vig_predict_1=="`v'"
	local ctr_temp=`ctr_temp'+1
	}

label define S1exppunish 1 "Nothing" 2 "Escorted from park" 3 "Verbal Warn" 4 "Citation" 5 "Arrested"
label values S1out_expectedpunish_vig1 S1exppunish
label variable S1out_expectedpunish_vig1 "Severity of expected Civilian punishment (1-5)"
tab S1out_expectedpunish_vig1

* Fairness to Civilian

tab vig_fair_1
local values_temp `" "Unfairly" "Somewhat unfairly" "Neither unfairly nor fairly" "Somewhat fairly" "Fairly" "'
local ctr_temp=1
gen S1out_fairness_cit_vig1 = .
foreach v of local values_temp {
	di "--`v'--"
	replace S1out_fairness_cit_vig1=`ctr_temp' if vig_fair_1=="`v'"
	local ctr_temp=`ctr_temp'+1
	}

label define fairness 1 "Unfair" 2 "Somewhat Unfair" 3 "Neither" 4 "Somewhat fair" 5 "Fair"
label values S1out_fairness_cit_vig1 fairness
label variable S1out_fairness_cit_vig1 "Fairness to Civilian (prior to learning punishment)"

* Fairness of punishment

tab vig_fair_ticket_1
local values_temp `" "Unfair" "Somewhat unfair" "Neither unfair nor fair" "Somewhat fair" "Fair" "'
local ctr_temp=1
gen S1out_fair_pun_vig2 = .
foreach v of local values_temp {
	di "--`v'--"
	replace S1out_fair_pun_vig2=`ctr_temp' if vig_fair_ticket_1=="`v'"
	local ctr_temp=`ctr_temp'+1
	}

label values S1out_fair_pun_vig2 fairness
label variable S1out_fair_pun_vig2 "Fairness to Civilian of assigned punishment (1-5)"

* Preferred punishment

tab vig_should_1
local values_temp `" "Left after learning the gunshot call was a false alarm, and not approached Parsons" "Left the park after speaking with Parsons" "Escorted Parsons from the park and left" "Escorted Parsons from the park and issued him a verbal warning for disorderly conduct" "Escorted Parsons from the park and issued him a ticket for disorderly conduct" "Escorted Parsons from the park and arrested him for disorderly conduct" "'
local ctr_temp=1
gen S1out_prefpunish_vig2 = .
foreach v of local values_temp {
	di "--`v'--"
	replace S1out_prefpunish_vig2=`ctr_temp' if vig_should_1=="`v'"
	local ctr_temp=`ctr_temp'+1
	}

label define S1labshouldpunish 1 "None (Never Approach)" 2 "None (Speak and leave)" 3 "None (Escort from Park)" 4 "Verbal warning" 5 "Ticket" 6 "Arrest"
label values S1out_prefpunish_vig2 S1labshouldpunish
label variable S1out_prefpunish_vig2 "Preferred Punishment (1-6)"

* Global fairness

tab vig_global_fair
local values_temp `" "Unfair" "Somewhat unfair" "Neither unfair nor fair" "Somewhat fair" "Fair" "'
local ctr_temp=1
gen S1out_fair_global_vig2 = .
foreach v of local values_temp {
	di "--`v'--"
	replace S1out_fair_global_vig2=`ctr_temp' if  vig_global_fair=="`v'"
	local ctr_temp=`ctr_temp'+1
	}

label values S1out_fair_global_vig2 fairness
label variable S1out_fair_global_vig2 "Overall fairness to Civilian of interaction (1-5)"

tab S1out_fair_global_vig2
tab S1out_fair_global_vig2, nolabel

* Restrict sample to people who answer at least one outcome question
keep if S1out_expectedpunish_vig1 ~=. | S1out_fairness_cit_vig1 ~=. | S1out_fair_global_vig2~=. | S1out_fair_pun_vig2!=. | S1out_prefpunish_vig2 !=.

* Sample demographics

gen x_age = real(age)

gen x_education = ""
replace x_education = "Some high school or less" if education=="1" | education=="2" | education=="3"
replace x_education = "High school graduate" if education=="4"
replace x_education = "Other post high school vocational training" if education=="5"
replace x_education = "Completed some college, but no degree" if education=="6"
replace x_education = "Associate's degree" if education=="7"
replace x_education = "Bachelor's degree" if education=="8" | education=="9"
replace x_education = "Master's or professional degree"  if education=="10"
replace x_education = "Doctorate degree"  if education=="11"

gen x_female = (gender=="2")

gen x_region = ""
replace x_region= "Northeast" if region=="1"
replace x_region= "Midwest" if region=="2"
replace x_region= "South" if region=="3"
replace x_region= "West" if region=="4"

gen x_pid_3="Independent"
replace x_pid_3 = "Democrat" if dem_pid=="Democrat" | dem_lean=="The Democratic Party"
replace x_pid_3 = "Republican" if dem_pid=="Republican" | dem_lean=="The Republican Party"

gen x_hhi=int(real(hhi))
replace x_hhi =3 if x_hhi ==1 | x_hhi ==2
replace x_hhi=x_hhi-2
tab x_hhi

gen racetemp=int(real(ethnicity))
gen x_race="Other"
replace x_race="White" if racetemp==1
replace x_race="Black" if racetemp==2
replace x_race="AAPI" if racetemp>=4 & racetemp<=14
replace x_race="Hispanic" if hispanic~="1" & hispanic~="16"
drop racetemp

* Remove further uncessary variables, identifying information
drop recipientlastname-userlanguage meta_operatingsystem meta_resolution meta_browser meta_version zip
drop status responseid  ipaddress intro_prompt rid useragent supname supplier_id updated treat_vignette-policy_q *timer* falk* mrel* mjud* contact_past contact_f2f contact_arrest contact_quantity safety_walk victim_crime victim_report vig_fair_binary pew* deviceidentifier deviceidentifier

compress

save Study1data_publicdatafile_cleaned.dta, replace

log close
